﻿using System;
using System.Runtime.InteropServices;

namespace ConsoleApp.Test
{
    unsafe internal class TestNativeMemory
    {
        public static void Test()
        {
            int count = 4;
            nuint byteLen = (nuint)((ulong)(sizeof(int) * count));
            int* ptr1 = (int*)NativeMemory.Alloc(byteLen);
            int* ptr2 = (int*)NativeMemory.Alloc(byteLen);

            Console.WriteLine("---------- Alloc -----------------");
            for (int i = 0; i < count; i++)
            {
                ptr1[i] = i;
            }
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(ptr1[i]);
            }


            Console.WriteLine("---------- Copy -----------------");
            NativeMemory.Copy(ptr1, ptr2, byteLen);
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(ptr2[i]);
            }

            Console.WriteLine("---------- Clear -----------------");
            NativeMemory.Clear(ptr2, byteLen);
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(ptr2[i]);
            }

            Console.WriteLine("---------- Fill -----------------");
            NativeMemory.Fill(ptr2, byteLen, 5);
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(ptr2[i]);
            }

            Console.WriteLine("---------- Realloc -----------------");
            int count2 = count * 2;
            nuint byteLen2 = (nuint)((ulong)(sizeof(int) * count2));
            int* ptr3 = (int*)NativeMemory.Realloc(ptr2, byteLen2);
            for (int i = 0; i < count2; i++)
            {
                ptr3[i] = i;
            }

            for (int i = 0; i < count2; i++)
            {
                Console.WriteLine(ptr3[i]);
            }
        }

    }
}
